//
//  CTWelcomeView.m
//  CamTalk
//
//  Created by dujiepeng on 14-7-14.
//  Copyright (c) 2014年 dujiepeng. All rights reserved.
//

#import "CTWelcomeView.h"

@interface CTWelcomeView () <UIScrollViewDelegate>{
    NSArray *_images;
    float _offset;
}

@end

@implementation CTWelcomeView

- (id)initWithFrame:(CGRect)frame images:(NSArray *)images
{
    CGRect selfFrame = frame;
    if ([[[UIDevice currentDevice] systemVersion] integerValue] < 7.0) {
        selfFrame.origin.y -= 20;
    }
    self = [super initWithFrame:selfFrame];
    if (self) {
        _images = images;
        self.pagingEnabled = YES;
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        self.bounces = NO;
        self.contentSize = CGSizeMake(self.frame.size.width * images.count, self.frame.size.height);
        if (images.count == 1) {
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                           initWithTarget:self
                                           action:@selector(tapHiddenActions:)];
            
            CGRect rect = CGRectMake(0, 0,self.frame.size.width, self.frame.size.height);
            UIImageView * imageView = [[UIImageView alloc]initWithFrame:rect];
            imageView.contentMode = UIViewContentModeScaleToFill;
            imageView.image = [images objectAtIndex:0];
            [self addGestureRecognizer:tap];
            [self addSubview:imageView];

        }else {
            for (int i = 0; i < images.count; i++) {
                CGRect rect = CGRectMake(self.frame.size.width * i, 0,self.frame.size.width, self.frame.size.height);
                UIImageView * imageView = [[UIImageView alloc]initWithFrame:rect];
                imageView.contentMode = UIViewContentModeScaleToFill;
                imageView.image = [images objectAtIndex:i];
                [self addSubview:imageView];
            }
        }
    }
    
    self.delegate = self;
    return self;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    _offset = scrollView.contentOffset.x / scrollView.frame.size.width;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView
                  willDecelerate:(BOOL)decelerate{
    if (_offset > 1 ) {
        self.bounces = YES;
    }else {
        self.bounces = NO;
    }
    
    if (_offset > _images.count - 1 + 0.1) {
        CGAffineTransform moveLeft =
        CGAffineTransformTranslate(CGAffineTransformIdentity, -self.frame.size.width, 0);
        
        __weak CTWelcomeView *weakSelf = self;
        [UIView animateWithDuration:0.25 animations:^{
            weakSelf.transform = moveLeft;
        } completion:^(BOOL finished) {
            [weakSelf removeFromSuperview];
        }];
    }
}

- (void)tapHiddenActions:(UITapGestureRecognizer *)tap{
    CGAffineTransform changTransform = CGAffineTransformMakeScale(2, 2);
    __weak CTWelcomeView *weakSelf = self;
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.transform = changTransform;
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        [weakSelf removeFromSuperview];
    }];
}

-(void)dealloc{
    NSLog(@"welcome delloc");
}

@end
